unit FListProduccion;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, XDBGrids, StdCtrls, DBCtrls, Buttons, ExtCtrls, DB,
  DBGrids, ZAbstractRODataset, StrUtils, ComCtrls, DateUtils, ImgList;

type
  TFormListProduccion = class(TForm)
    Label1: TLabel;
    pBotones: TPanel;
    BtnSalir: TSpeedButton;
    Grid: TXDBGrid;
    dsListProduc: TDataSource;
    BtnImp_Orden_Trab: TSpeedButton;
    cbOcultarHechos: TCheckBox;
    cbMostrarPdtes: TCheckBox;
    Label2: TLabel;
    Fecha_Ini: TDateTimePicker;
    Label3: TLabel;
    Fecha_Fin: TDateTimePicker;
    ImageList: TImageList;
    BtnOrden: TSpeedButton;
    BtnUp: TSpeedButton;
    BtnDown: TSpeedButton;
    Label4: TLabel;
    cbMaquina: TComboBox;
    BtnImp_Listado: TSpeedButton;
    cbNoIncluir: TCheckBox;
    gbListasAnt: TGroupBox;
    cbListasAnt: TComboBox;
    procedure FormShow(Sender: TObject);
    procedure BtnSalirClick(Sender: TObject);
    procedure GridTitleClick(Column: TXColumn);
    procedure GridPaintColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TXColumn; Highlight: Boolean;
      Selections: TSelections; var Color: TColor; Font: TFont;
      var Image: TPersistent);
    procedure BtnImp_Orden_TrabClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Fecha_Change(Sender: TObject);
    procedure CheckBoxClick(Sender: TObject);
    procedure BtnOrdenClick(Sender: TObject);
    procedure BtnUpClick(Sender: TObject);
    procedure BtnDownClick(Sender: TObject);
    procedure ComboBoxChange(Sender: TObject);
    procedure BtnImp_ListadoClick(Sender: TObject);
    procedure GridDblClick(Sender: TObject);
  private
    { Private declarations }
    Procedure RefrescaLista;
    Procedure RefrescaQuery;
    Procedure ListasAnteriores;
  public
    { Public declarations }
  end;

var
  FormListProduccion: TFormListProduccion;

implementation

uses
  FMain,
  FPresupuestosEdit,
  modDatos;

{$R *.dfm}

procedure TFormListProduccion.FormShow(Sender: TObject);
var
  StrSQL: String;
begin
  Left  := 0;
  Top   := FormMain.Height;
  Width := Screen.Width;
  Height:= Screen.WorkAreaHeight - Top;

  StrSQL := 'SELECT ID_MAQUINA, DESCRIPCION ' +
            'FROM MAQUINAS ';
  md.AbreQuery( md.Query, StrSQL);
  cbMaquina.Items.Add( '0 Todas');
  While not md.Query.Eof do
  begin
    cbMaquina.Items.Add( md.Query.Fields[ 0].AsString + ' ' + md.Query.Fields[ 1].AsString);
    md.Query.Next;
  end;
  md.Query.Close;
  cbMaquina.ItemIndex := 0;
  cbMaquina.Tag       := 1;

  ListasAnteriores;
  cbListasAnt.Tag := 1;

  cbNoIncluir.Tag     := 1;
  cbOcultarHechos.Tag := 1;
  cbMostrarPdtes.Tag  := 1;

  Fecha_Ini.Date := Date;
  Fecha_Ini.Tag  := 1;
  Fecha_Fin.Date := IncDay( Date, 7);
  Fecha_Fin.Tag  := 1;
  if not md.qList_Produc.Active then
    RefrescaQuery;

end;

procedure TFormListProduccion.BtnSalirClick(Sender: TObject);
begin
  Close;
end;

procedure TFormListProduccion.GridTitleClick(Column: TXColumn);
begin
  MD.qList_Produc.SortedFields := Grid.OrderFields;
end;

procedure TFormListProduccion.BtnImp_Orden_TrabClick(Sender: TObject);
begin
  md.Imprimir_Orden_Trabajo( md.qList_Produc.FieldByName( 'ID_PRESUP').AsString);
end;


procedure TFormListProduccion.RefrescaLista;
var
  No    : Integer;
begin
  No     := md.qList_Produc.RecNo;
  md.qList_Produc.Close;
  md.qList_Produc.Open;
  md.qList_Produc.RecNo := No;
end;

procedure TFormListProduccion.GridPaintColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TXColumn;
  Highlight: Boolean; Selections: TSelections; var Color: TColor;
  Font: TFont; var Image: TPersistent);
begin
  if (slCellSelected in Selections) or (slCellFixed in Selections) then Exit
  else
    //Cada Registro le damos un color diferente
    if slMultiSelected in Selections then //Color := $00F08000
      if Odd( TXDBGrid( Sender).DataSource.DataSet.RecNo) then Color := $0AF0047
                                                          else Color := $0FF0000
    else
      //Cada Registro le damos un color diferente
      if Odd( TXDBGrid( Sender).DataSource.DataSet.RecNo) then Color := $0E7FFFF
                                                          else Color := $0C7FFFF
end;

procedure TFormListProduccion.FormCreate(Sender: TObject);
begin
  if md.qList_Produc.Active then md.qList_Produc.Close;
  dsListProduc.DataSet := md.qList_Produc;
end;

procedure TFormListProduccion.RefrescaQuery;
var
  StrSQL: String;
begin
  if cbListasAnt.ItemIndex = 0 then
  begin
    StrSQL := '( PED.FECHA_PREVISTA BETWEEN STR_TO_DATE( ' + QuotedStr( FormatDateTime( 'dd/mm/yyyy', Fecha_Ini.Date)) + ',''%d/%m/%Y'') AND STR_TO_DATE( ' + QuotedStr( FormatDateTime( 'dd/mm/yyyy', Fecha_Fin.Date)) + ',''%d/%m/%Y'')) ';
    if cbMostrarPdtes.Checked then
      StrSQL := '(' + StrSQL + 'OR ( PED.HECHO = 0 AND PED.FECHA_PREVISTA <= STR_TO_DATE( ' + QuotedStr( FormatDateTime( 'dd/mm/yyyy', Fecha_Ini.Date)) + ',''%d/%m/%Y''))) ';
    if cbNoIncluir.Checked then
      StrSQL := StrSQL + ' AND ORDEN_PROD = 0 ';
    if cbOcultarHechos.Checked then
      StrSQL := StrSQL + ' AND PED.HECHO = 0 ';
    if cbMaquina.ItemIndex > 0 then
      StrSQL := StrSQL + ' AND PRE.ID_MAQUINA = ' + LeftStr( cbMaquina.Text, 2) + ' ';
  end
  else
    begin
      StrSQL := '( PED.ORDEN_PROD DIV 1000 = ' + LeftStr( cbListasAnt.Text, 5) + ') ';
    end;


  StrSQL := 'SELECT  PED.HECHO, PED.ORDEN_PROD, PED.ID_PRESUP, PED.ID_PEDIDO, PED.LAB_REFERENCIA, CLI.ID_CLIENTE, CLI.NOMBRE, PED.MODELO, ' +
                   'CASE ' +
                        'WHEN FAB_ALTO IN ( ''2'', ''3'', ''4'', ''6'', ''8'', ''12'', ''24'') THEN 24 ' +
                        'WHEN FAB_ALTO IN ( ''5,5'', ''7 1/3'', ''11'', ''22'') THEN 22 ' +
                        'WHEN FAB_ALTO IN ( ''3 1/3'', ''10'', ''20'') THEN 20 ' +
                        'WHEN FAB_ALTO IN ( ''8,5'', ''17'') THEN 17 ' +
                    'ELSE FAB_ALTO END TAM_ALTO, ' +
                    'PRE.FAB_ANCHO, PRE.NUM_TANTOS, PED.CANTIDAD, PED.FECHA_PEDIDO, PED.FECHA_PREVISTA, PED.TIEMPO_MAQUINA, PED.TIEMPO_COLECTORA, PED.TIEMPO_MANIPULADO, ' +
                    '(SELECT SUM( TOTAL) ' +
                    'FROM PRESUP_CALCULOS ' +
                    'WHERE ID_PRESUP_CANTIDAD = ( SELECT ID_PRESUP_CANTIDAD ' +
                                                'FROM PRESUP_CANTIDADES ' +
                                                'WHERE ID_PRESUP = PRE.ID_PRESUP AND ACEPTADO = 1) AND ID_PAPEL IS NOT NULL AND ID_ITEM = 0) IMP_PAPEL, ' +
                    '(SELECT SUM( TOTAL) ' +
                    'FROM PRESUP_CALCULOS ' +
                    'WHERE ID_PRESUP_CANTIDAD = ( SELECT ID_PRESUP_CANTIDAD ' +
                                                'FROM PRESUP_CANTIDADES ' +
                                                'WHERE ID_PRESUP = PRE.ID_PRESUP AND ACEPTADO = 1) AND ID_ITEM = -1) IMP_COMISION, ' +
                    '( SELECT DESCRIPCION FROM MAQUINAS WHERE ID_MAQUINA = PRE.ID_MAQUINA) MAQUINA, ' +
                    QuotedStr( FormatDateTime( 'dd/mm/yyyy', Fecha_Ini.Date)) + ' DESDE, ' + QuotedStr( FormatDateTime( 'dd/mm/yyyy', Fecha_Fin.Date)) + ' HASTA ' +
            'FROM PEDIDOS PED ' +
               'INNER JOIN PRESUPUESTOS PRE ON PED.ID_PRESUP = PRE.ID_PRESUP ' +
               'INNER JOIN CLIENTES CLI ON CLI.ID_CLIENTE = PRE.ID_CLIENTE ' +
            'WHERE ' + StrSQL +
            'ORDER BY PED.ORDEN_PROD, PED.FECHA_PREVISTA, TAM_ALTO';
  md.AbreQuery( md.qList_Produc, StrSQL);
end;

procedure TFormListProduccion.Fecha_Change(Sender: TObject);
begin
  if TDateTimePicker( Sender).Tag = 1 then
    RefrescaQuery;
end;

procedure TFormListProduccion.CheckBoxClick(Sender: TObject);
begin
  if TCheckBox( Sender).Tag = 1 then
    RefrescaQuery;
end;

procedure TFormListProduccion.ComboBoxChange(Sender: TObject);
begin
  if TComboBox( Sender).Tag = 1 then
      RefrescaQuery;
end;


procedure TFormListProduccion.BtnOrdenClick(Sender: TObject);
var
  Num   : Integer;
  StrSQL: String;
begin
  Num := StrToInt( IntToStr( Yearof( Date) - 2000) + FormatFloat( '000', DayOfTheYear( Date)) + '001');
  md.qList_Produc.DisableControls;
  md.qList_Produc.First;
  While not md.qList_Produc.Eof do
  begin
    StrSQL := 'UPDATE PEDIDOS SET ORDEN_PROD = ' + IntToStr( Num) + ' WHERE ID_PEDIDO = ' + md.qList_Produc.FieldByName( 'ID_PEDIDO').AsString;
    md.Ejecuta( StrSQL);
    md.qList_Produc.Next;
    Num := Num + 1;
  end;
  Grid.OrderFields := '';
  MD.qList_Produc.SortedFields := '';
  RefrescaLista;
  md.qList_Produc.EnableControls;
end;

procedure TFormListProduccion.BtnUpClick(Sender: TObject);
var
  StrSQL : String;
begin
  if ( md.qList_Produc.RecNo > 1) and ( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger > 0) then
  begin
    StrSQL := 'UPDATE PEDIDOS SET ORDEN_PROD = ' + md.qList_Produc.FieldByName( 'ORDEN_PROD').AsString + ' WHERE ORDEN_PROD = ' + IntToStr( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger - 1) + ';' +
              'UPDATE PEDIDOS SET ORDEN_PROD = ' + IntToStr( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger - 1) + ' WHERE ID_PEDIDO = ' + md.qList_Produc.FieldByName( 'ID_PEDIDO').AsString;
    md.Ejecuta( StrSQL);
    RefrescaLista;
    md.qList_Produc.RecNo := md.qList_Produc.RecNo - 1;
  end;
end;

procedure TFormListProduccion.BtnDownClick(Sender: TObject);
var
  StrSQL : String;
begin
  if ( md.qList_Produc.RecNo < md.qList_Produc.RecordCount) and ( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger > 0) then
  begin
    StrSQL := 'UPDATE PEDIDOS SET ORDEN_PROD = ' + md.qList_Produc.FieldByName( 'ORDEN_PROD').AsString + ' WHERE ORDEN_PROD = ' + IntToStr( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger + 1) + ';' +
              'UPDATE PEDIDOS SET ORDEN_PROD = ' + IntToStr( md.qList_Produc.FieldByName( 'ORDEN_PROD').AsInteger + 1) + ' WHERE ID_PEDIDO = ' + md.qList_Produc.FieldByName( 'ID_PEDIDO').AsString;
    md.Ejecuta( StrSQL);
    RefrescaLista;
    md.qList_Produc.RecNo := md.qList_Produc.RecNo + 1;
  end;
end;

procedure TFormListProduccion.BtnImp_ListadoClick(Sender: TObject);
var
  Num: Integer;
begin
  Num := md.qList_Produc.RecNo;
  md.qList_Produc.DisableControls;
  md.frxReport.LoadFromFile( 'Rpt_List_Produc_R.fr3');
  md.frxReport.PrepareReport();
  md.frxReport.ShowPreparedReport;
  md.qList_Produc.EnableControls;
  md.qList_Produc.RecNo := Num;
end;

procedure TFormListProduccion.GridDblClick(Sender: TObject);
begin
  With TFormPresupuestosEdit.Create( Self) do
  try
    md.TPresupuestos.Open;
    md.TPresupuestos.Locate( 'ID_PRESUP', md.qList_Produc.FieldByName( 'ID_PRESUP').Value, []);
    md.TPresupuestos.Edit;
    tsCalculos.TabVisible := False;

    ShowModal;
  finally
    Free;
    md.TPresupuestos.Close;
    RefrescaLista;
  end;
end;

procedure TFormListProduccion.ListasAnteriores;
var
  StrSQL: String;
begin
  StrSQL := 'SELECT DISTINCT ORDEN_PROD DIV 1000 ' +
            'FROM PEDIDOS ' +
            'ORDER BY ORDEN_PROD DESC ';
  md.AbreQuery( md.Query, StrSQL);
  cbListasAnt.Items.Add( 'SIN INCLUIR EN LISTAS ANT.');
  While not md.Query.Eof do
  begin
    if md.Query.Fields[ 0].AsInteger > 0 then
    begin
      cbListasAnt.Items.Add( md.Query.Fields[ 0].AsString + ' ' + FormatDateTime( 'dd/mm/yyyy', IncDay( EncodeDate( StrToInt( '20' + LeftStr( md.Query.Fields[ 0].AsString, 2)), 1, 1),  StrToInt( RightStr( md.Query.Fields[ 0].AsString, 3)) - 1 )));
    end;
    md.Query.Next;
  end;
  md.Query.Close;
  cbListasAnt.ItemIndex := 0;
end;

end.
